﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Bussiness;

namespace GUI {
    public partial class frmQuanLyKhachHang : Form {
        public frmQuanLyKhachHang() {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e) {
            Close();
        }

        private void btnTim_Click(object sender, EventArgs e) {
            KhachHangService service = new KhachHangService();

            string tenKhachHang = "";
            string maKhachHang = "";
            string diaChi = "";
            string email = "";
            string dienThoai = "";

            if (chkHoTen.Checked)
                tenKhachHang = txtHoTen.Text.Trim();
            if (chkMaKhachHang.Checked)
                maKhachHang = txtMaKhachHang.Text.Trim();
            if (chkEmail.Checked)
                email = txtEmail.Text.Trim();
            if (chkDiaChi.Checked)
                diaChi = txtDiaChi.Text.Trim();
            if (chkDienThoai.Checked)
                dienThoai = txtDienThoai.Text.Trim();

            dataGridView1.DataSource = service.SearchKhachHang(tenKhachHang, maKhachHang, diaChi, dienThoai, email);           
        }

        private void frmQuanLyKhachHang_Load(object sender, EventArgs e) {
            idKhachHangColumn.Visible = false;

            idKhachHangColumn.DataPropertyName = KhachHangColumn.IDKhachHang;
            hoTenColumn.DataPropertyName = KhachHangColumn.HoTen;
            diaChiColumn.DataPropertyName = KhachHangColumn.DiaChi;
            dienThoaiColumn.DataPropertyName = KhachHangColumn.DienThoai;
            maKhachHangColumn.DataPropertyName = KhachHangColumn.MaKhachHang;
            emailColumn.DataPropertyName = KhachHangColumn.Email;
            tienNoColumn.DataPropertyName = KhachHangColumn.TienNo;
        }

        private void btnCapNhat_Click(object sender, EventArgs e) {
            if (dataGridView1.RowCount == 0)
                return;

            List<KhachHangDTO> khachHangList = new List<KhachHangDTO>();
            for (int i = 0; i < dataGridView1.RowCount; i++) {
                KhachHangDTO khachHang = new KhachHangDTO();
                khachHang.IDKhachHang = Convert.ToInt32(dataGridView1.Rows[i].Cells["idKhachHangColumn"].Value);
                khachHang.HoTen = dataGridView1.Rows[i].Cells["hoTenColumn"].Value.ToString();
                khachHang.DiaChi = dataGridView1.Rows[i].Cells["diaChiColumn"].Value.ToString();
                khachHang.DienThoai = dataGridView1.Rows[i].Cells["dienThoaiColumn"].Value.ToString();
                khachHang.Email = dataGridView1.Rows[i].Cells["emailColumn"].Value.ToString();
                khachHang.TienNo = Convert.ToInt32(dataGridView1.Rows[i].Cells["tienNoColumn"].Value);

                khachHangList.Add(khachHang);
            }

            KhachHangService service = new KhachHangService();
            if (service.UpdateKhachHang(khachHangList))
                MessageBox.Show("Cập nhật thành công");
            else
                MessageBox.Show("Có lỗi, vui lòng thử lại");
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
